<script lang="ts" setup>
import Wx from 'wx-jssdk'
import { getWxLoginConfig, scanLogin } from '~/api/login'

const { wxLogin } = useUserStore()
const route = useRoute()
// 用户扫码授权处理
function scanAuth(code: string, state: string) {
  // 扫码登录 页面回调
  wxLogin(code, state)
}

function loadLoginQrcode() {
  getWxLoginConfig().then((data) => {
    if (data?.appid) {
      Wx.config({
        self_redirect: false,
        id: 'login_container',
        appid: data.appid,
        scope: 'snsapi_login',
        redirect_uri: data.callbackurl,
        state: data.state,
        style: 'black',
      })
    }
  })
}
onMounted(() => {
  if (route.query.code && route.query.state) {
    // 登录
    scanAuth(route.query.code, route.query.state)
  }
  else {
    // 生成二维码
    loadLoginQrcode()
  }
})
</script>

<template>
  <div id="login_container" class="qrcode" />
</template>

<style>

</style>
